<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\User;
use App\Http\model\Dictionary;

class UserController extends Controller
{


	/**
	 * [login 登录]
	 * @Author   孙丽男
	 * @DateTime 2018-06-04
	 * @param    [用户账号]  str  $number
     * @param    [用户密码]  str  $password
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function login(Request $request){

    	$result = array();

    	if ($request['number'] && $request['password']) {
    		
    		$where['number'] = $request['number'];

            $where['password'] = "###" . md5(md5(str_replace( " ", "", $request['password'])));

    		$user = new User;

    		$result = $user->loginModel($where);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}

	}

    /**
     * [showDepart 部门列表]
     * @Author   孙丽男
     * @DateTime 2018-06-04
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function departList(){
        
        $user = new User;

        $result = $user->departListModel();

        return $result;
    }

    /**
     * [zidian 读取字典数据]
     * @Author   孙丽男
     * @DateTime 2018-06-11
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function zidian(Request $request){
        $result = array();
        if ($request['id']) {
            $zidian = new Dictionary;
            $zidian = $zidian->contents($request['id']);
            return json_encode($zidian);
        }else{
            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }
	/**
	 * [addUser 添加用户]
	 * @Author   孙丽男
	 * @DateTime 2018-06-04
	 * @param    [用户信息]  array  $userArr
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
    public function addUser(Request $request){

    	$result = array();

    	if ($request['number'] && $request['name'] && $request['sex'] && $request['phone'] && $request['role_id'] && $request['department']) {
    		
    		$userArr = $request->all();
            if (isset($userArr['s'])) {
                $userArr = array_except($userArr, ['s']);
            }
            print_r($userArr);die;
    		$user = new User;

    		$result = $user->addUserModel($userArr);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}

    }
    /**
     * [findUser 用户列表]
     * @Author   孙丽男
     * @DateTime 2018-06-04
     * @version  6.2
     * @return   [json]    [$result]
     */
    public function findUser(Request $request){

    	//构建查询条件
    	$where = array();

        if ($request['userId']) {
            
            $where = [
                ['user.id', $request['userId']],
            ]; 
        }

    	if ($request['name']) {
    		
    		$where[1] = ['user.name', 'like', '%' . $request['name'] . '%'];
    	}

    	if ($request['start_time']) {
    		
    		$where[2] =  ['user.update_time', '>',  $request['start_time']];
    	}

    	if ($request['end_time']) {
    		
    		$where[3] =  ['user.update_time', '<',  $request['end_time']];
    	}

    	$user = new User;

    	$result = $user->findUserModel($where);

    	return $result;
    }

    /**
	 * [deleteUserModel 删除用户]
	 * @Author   孙丽男
	 * @DateTime 2018-06-04
	 * @param    [用户id] array $userId[]
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
    public function deleteUser(Request $request){

    	$result = array();
    
    	if (is_array($request['userId'])) {

            $userArr = $request->all();
            if (isset($userArr['s'])) {
                $userArr = array_except($userArr, ['s']);
            }

    		$userIdArr = $userArr['userId'];

    		$user = new User;

    		$result = $user->deleteUserModel($userIdArr);

    		return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);
    	}
    }

    /**
     * [updateUser 修改用户信息（不包括用户角色）]
     * @Author   孙丽男
     * @DateTime 2018-06-04
     * @param    [用户id]  int $userId
     * @param    [修改的信息]  ....
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function updateUser(Request $request){

    	$userId = $request['userId'];

    	if ($userId) {
    		
    		$updateInfo = $request->all();
            if (isset($updateInfo['s'])) {
                $updateInfo = array_except($updateInfo, ['s']);
            }

    		unset($updateInfo['userId']);

            if ($updateInfo) {
                
                $user = new User;
                $result = $user->updateUserModel($userId, $updateInfo);
                return $result;

            }else{

                $result['code'] = 1002;
                $result['msg'] = '请填写修改数据';
                return json_encode($result);
            }

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);
    	}
    }

     /**
     * [updateStatus 修改用户状态]
     * @Author   孙丽男
     * @DateTime 2018-06-04
     * @param    [用户id组]  array $userId[]
     * @param    [状态]  int  $status
     * @version  6.2
     * @return   [json]     [$result]
     */    
    public function updateStatus(Request $request){

        $userId = $request['userId'];

        if (is_array($userId) && $request['status']) {
            
            $user = new User;
            $result = $user->updateStatusModel($userId, $request['status']);
            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [rolesList 角色列表树状显示]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @version  6.2
     * @return   [josn]     [$result]
     */
    public function rolesList(){
            
            $user = new User;
            $result = $user->rolesListModel();
            return $result;
    }
    /**
     * [setRoles 为用户设置角色]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @param    [用户id] array $userId[]
     * @param    [角色id] array $roleId[]
     * @version  6.2
     */
    public function setRoles(Request $request){

        $result = array();

        if (is_array($request['userId']) && is_array($request['roleId'])) {
            
            $user = new User;

            $result = $user->setRolesModel($request['userId'], $request['roleId']);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [loginOut 退出登录]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @param    [用户id] int  userId
     * @version  6.2
     * @return   [type]     [description]
     */
    public function loginOut(Request $request){

        $result = array();
        $res = $request->session()->forget('uid');

        $result['code'] = 1000;
        $result['msg'] = '退出成功';
        return json_encode($result);
       
    }

    /**
     * [updatePass 修改密码]
     * @Author   孙丽男
     * @DateTime 2018-06-05
     * @param    [用户id] int $userId
     * @param    [用户密码] int $password
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function updatePass(Request $request){
        $result = array();
        if ($request['password'] && $request['newpassword']) {
            if ($request['password'] != $request['newpassword']) {
                $result['code'] = 1003;
                $result['msg'] = '密码不同';
                return json_encode($result);
            }
           $user = new user;
           $result = $user->updatePassModel($request['newpassword']);
           return $result;
        }else{
            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [addDepart 添加部门]
     * @Author   孙丽男
     * @DateTime 2018-06-13
     * @param    [type]
     * @version  6.2
     */
    public function addDepart(Request $request){

        if ($request['abbreviation'] && $request['fullname'] && $request['pid'] && $request['sort']) {
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            $user = new user;
            $result = $user->addDepartModel($data);
            return $result;
        }else{
            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [findDepart 部门列表]
     * @Author   孙丽男
     * @DateTime 2018-06-13
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function findDepart(Request $request){

        //构建查询条件
        $where = [
                ['department.pid', '<>',0],
            ]; 

        if ($request['departId']) {
            
            $where = [
                ['department.id', $request['departId']],
            ]; 
        }

        if ($request['fullname']) {
            
            $where[1] = ['department.fullname', 'like', '%' . $request['fullname'] . '%'];
        }

        if ($request['start_time']) {
            
            $where[2] =  ['department.update_time', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['department.update_time', '<',  $request['end_time']];
        }

        $user = new User;

        $result = $user->findDepartModel($where);

        return $result;

    }

    /**
     * [deleteDarpt 删除部门]
     * @Author   孙丽男
     * @DateTime 2018-06-13
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function deleteDarpt(Request $request){

        $result = array();
    
        if (is_array($request['departId'])) {

            $departIdArr = $request->get('departId');

            $user = new User;

            $result = $user->deleteDepartModel($departIdArr);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

     /**
     * [updateDepart 修改部门]
     * @Author   孙丽男
     * @DateTime 2018-06-13
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function updateDepart(Request $request){

       if ($request['abbreviation'] && $request['fullname'] && $request['pid'] && $request['sort'] && $request['departId']) {
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            $departId =  $request['departId'];
            unset($data['departId']);
            $user = new user;
            $result = $user->updateDepartModel($departId,$data);
            return $result;
        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }
}
